/*
 * Created on May 29, 2004
 */
package dk.aiszone.lang.utils;

import java.util.Iterator;
import java.util.Map;

/**
 * A set of helper methods supports <code>Maps</code>.
 * 
 * @author <a href="mailto:ais@mail.dk">Ais</a>
 * @since 1.0
 * @version 1.0
 */
public class MapUtils {

    /**
     * Makes a <code>toString</code> representation of the supplied <code>Map</code>.<br>
     * 
     * @param map
     *            The <code>Map</code> to make a <code>String</code> representation of.
     * @return The <code>String</code> representation of the <code>Map</code>.
     */
    public static String toString(Map<? extends Object, ? extends Object> map) {
        if (map == null) {
            return "";
        }

        StringBuffer buf = new StringBuffer();
        buf.append("{ ");

        Iterator<? extends Object> iter = map.keySet().iterator();
        while (iter.hasNext()) {
            Object key = iter.next();
            buf.append(toString(key, map.get(key)));

            if (iter.hasNext()) {
                buf.append(", ");
            }
        }

        buf.append(" }");

        return buf.toString();
    }

    private static String toString(Object key, Object value) {
        String tmp = "[" + key.toString() + " = ";

        if (value.getClass().isArray()) {
            tmp += "(" + ArrayUtils.arrayToString((Object[])value) + ")";
        } else {
            tmp += value.toString();
        }

        return tmp + "]";
    }
}
